'''
@Company: TWL
@Author: xue jian
@Email: xuejian@kanzhun.com
@Date: 2020-07-13 14:31:35
'''
import pymysql
import json
final = []
conn = pymysql.connect(
    host = "172.19.8.66",
    port = 3306,
    user = "algorithm",
    password = "S4!4Eywru@aH",
    # database = "rcd_phoenix_admin",
    database = "rcd_kraken",
    charset='utf8'
    )

cursor = conn.cursor()

# sql = "show tables"
# cursor.execute(sql)
# results = cursor.fetchall()
# print(results)


'''
@note: get the code dimension and value_type from feature_meta.
'''
req_fea = ["code", "feature_name", "value_type"]
req_ind = []
sql = "desc feature_meta"
cursor.execute(sql)

results = cursor.fetchall()
# print(results)
index = []
ind = 0
for result in results:
    index.append(result[0])
    if result[0] in req_fea:
        req_ind.append(ind)
    ind += 1
# print(req_ind)
# print(index)
final.append(index)
sql = "select * from feature_meta"
cursor.execute(sql)
results = list(cursor.fetchall())
# print(results[0])

for result in results:
    # print(result)
    final.append(list(result))

# print(final[0:3])

feature_meta = [[a[i] for i in req_ind] for a in final]
# for l in feature_meta:
#     print(l)
j_feature_store = {}
for l in feature_meta[1:]:
    j_feature_store[l[1]] = {"code":l[0], "value_type":l[2]}
# print(j_feature_store)
j_feature_store = json.dumps(j_feature_store)
f_res = open('./fea_code_map.json', 'w')
f_res.write(j_feature_store)
f_res.close()
